//app.js
App({
  onLaunch: function(options) {
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
    var that = this
    var shopId = options.shopId
    if (options.query.isShop != null) {
      that.globalData.isShop = options.query.isShop
    }
    if (shopId == null) {
      console.log('options null')
      shopId = extConfig.shopId
      if (shopId == null) {
        console.log('extConfig null')
        shopId = that.globalData.shopId
      }
    }
    var appid = options.appid
    if (appid == null) {
      appid = extConfig.appid
      if (appid == null) {
        appid = that.globalData.appid
      }
    }
    console.log(that.globalData.host)

    // var version = extConfig.version
    // that.globalData.version = version
    that.init()

    that.globalData.shopId = shopId
    that.globalData.appid = appid

    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.checkSession({
      success: function() {
        // console.log("check session success")
        //session 未过期，并且在本生命周期一直有效
      },
      fail: function() {
        console.log("check session fail")
        //登录态过期
        // wx.login() //重新登录
      }
    })
  },
  init() {
    var that = this;
    var host = that.globalData.host //'https://app.cenjiawen.com'
    var url = host + '/xhc/ticket/init/'
    that.request({
      url: url,
      data: {},
      success: function(res) {
        that.globalData.agreement = res.data.info.agreement
        that.globalData.notice = res.data.info.notice
        that.globalData.notification = res.data.info.notification
        that.globalData.images = res.data.info.images
        that.globalData.openTime = res.data.info.openTime
        that.globalData.closeTime = res.data.info.closeTime
      },
      fail: function(msg) {
        console.warn(msg)
      }
    })
  },
  getUserInfo: function(cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      that.serverLogin({
        success(){
          console.log('自动登录成功')
        },
        fail() {
          console.log('自动登录失败')
        }
      })
      return
      wx.login({
        success: function(res) {
          var code = res.code
          var host = that.globalData.host
          that.request({
            url: host + '/xhc/user/auth',
            data: {
              code: code
            },
            success: function(res) {
              var openid = res.data.info.openId
              var token = res.data.info.token
              // that.globalData.openId = openid
              // that.globalData.openid = openid
              that.globalData.token = token
              that.init()
              wx.setStorage({
                key: "openid",
                data: openid
              })
              wx.setStorage({
                key: "token",
                data: token
              })
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
          // wx.getUserInfo({
          //   success: function (res) {
          //     // console.log(res)
          //     var iv = res.iv
          //     var encryptedData = res.encryptedData
          //     // console.log('iv: ' + iv)
          //     that.globalData.userInfo = res.userInfo
          //     that.globalData.code = code
          //     that.globalData.iv = res.iv
          //     that.globalData.encryptedData = res.encryptedData
          //     var host = that.globalData.host
          //     that.request({
          //       url: host + '/xhc/ticket/userInfo',
          //       data: {
          //         iv: that.globalData.iv,
          //         encryptedData: that.globalData.encryptedData,
          //         code: that.globalData.code
          //       }, success: function (res) {
          //         var openid = res.data.info.openId
          //         var token = res.data.info.token
          //         that.globalData.openId = openid
          //         that.globalData.openid = openid
          //         that.globalData.token = token
          //         that.init()
          //         wx.setStorage({
          //           key: "openid",
          //           data: openid
          //         })
 
 c
 
 
 
 
 
 
          //         wx.setStorage({
          //           key: "token",
          //           data: token
          //         })
          //         typeof cb == "function" && cb(that.globalData.userInfo)
          //       }
          //     })
          //     typeof cb == "function" && cb(that.globalData.userInfo)
          //   }, fail() {

          //   }
          // })
        },
        fail(errMsg) {
          console.log(errMsg)
        }
      })
    }
  },
  globalData: {
    appid: 'wx6d47db34d18f5e3a',
    userInfo: null,
    iv: null,
    code: 'fffff',
    encryptedData: null,
    openid: '',
    openId: '',
    phone: '',
    // host: 'http://192.168.50.19:9090',
    // host: 'http://192.168.199.213:9090',
    // host:'https://app.zhen-yee.com',
    host: 'https://release.zhen-yee.com',
    windowWidth: null,
    shopId: 36,
    shop: null,
    adv: [],
    token: '',
    version: '1.0',
    isShop: null,
    themeColor: '#C1272D',
    defaultColor: '#929292',
    borderColor: '#f5f5f5',
    agreement: '',
    notice: '',
    notification: '',
    images: '',
    openTime: '',
    closeTime: ''
  },
  onShow: function() {
    var that = this;
    // that.getUserInfo()
    that.serverLogin({
      success() {
        console.log('自动登录成功')
      },
      fail() {
        console.log('自动登录失败')
      }
    })
  },
  serverLogin(param) {
    var that = this;
    if (that.globalData.openId){
      console.log('已登录')
      return
    }
    wx.login({
      success: function (res) {
        var code = res.code
        wx.getUserInfo({
          success: function (res) {
            var userInfo = res.userInfo
            var nickName = userInfo.nickName
            var avatarUrl = userInfo.avatarUrl
            var gender = userInfo.gender //性别 0：未知、1：男、2：女
            var province = userInfo.province
            var city = userInfo.city
            var country = userInfo.country


            var host = that.globalData.host
            that.request({
              url: host + '/xhc/user/auth',
              data: {
                code: code,
                nickName: nickName,
                avatarUrl: avatarUrl,
                avatarUrl: avatarUrl,
                gender: gender,
                province: province,
                country: country
              },
              success: function (res) {
                if (res.data.status != 200) {
                  param.fail()
                  return
                }
                var openid = res.data.info.openId
                var token = res.data.info.token
                that.globalData.openId = openid
                that.globalData.openid = openid
                that.globalData.token = token
                that.init()
                wx.setStorage({
                  key: "openid",
                  data: openid
                })
                wx.setStorage({
                  key: "token",
                  data: token
                })
                // typeof cb == "function" && cb(that.globalData.userInfo)
                param.success()
              }
            })
          }
        })
      },
      fail(errMsg) {
        console.log(errMsg)
        param.fail()
      }
    })
  },
  request: function(param) {
    var token = this.globalData.token
    var appid = this.globalData.appid
    var shopId = this.globalData.shopId
    var version = this.globalData.version
    var openId = this.globalData.openId
    if (param.data == null) {
      param.data = {
        token: token,
        appid: appid,
        version: version,
        openId: openId
      }
    } else {
      param.data['token'] = token
      param.data['appid'] = appid
      param.data['version'] = version
      param.data['openId'] = openId
    }
    return wx.request(param)
  }
})